package com.miniot.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miniot.base.common.ExcelExporter;
import com.miniot.base.entity.BoundDetail;
import com.miniot.base.entity.Devotion;
import com.miniot.base.mapper.DevotionMapper;
import com.miniot.base.service.BoundDetailService;
import com.miniot.base.service.DevotionService;
import com.miniot.base.vo.DevotionQueryVO;
import com.miniot.base.vo.ExportVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 投入品表
 *
 * @author zzq
 * @date 2023-10-17 11:15:55
 */
@Service
@AllArgsConstructor
@Slf4j
public class DevotionServiceImpl extends ServiceImpl<DevotionMapper, Devotion> implements DevotionService {

    private final BoundDetailService boundDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeDevotionById(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("id不能为空");
        }
        List<BoundDetail> list = boundDetailService.list(new LambdaQueryWrapper<BoundDetail>().eq(BoundDetail::getDevotionId, id));
        if (list.size()>0) {
            throw new RuntimeException("该投入品正在使用,不能删除");
        }
        this.removeById(id);
        return true;
    }

    @Override
    public ResponseEntity<byte[]> export(DevotionQueryVO devotion) throws IOException {
        List<Devotion> list = this.list(new LambdaQueryWrapper<Devotion>()
                .eq(ObjectUtils.isNotEmpty(devotion.getDevotionParentTypeId()),Devotion::getDevotionParentTypeId,devotion.getDevotionParentTypeId())
                .eq(ObjectUtils.isNotEmpty(devotion.getDevotionChildrenTypeId()),Devotion::getDevotionChildrenTypeId,devotion.getDevotionChildrenTypeId())
                .like(ObjectUtils.isNotEmpty(devotion.getDevotionName()), Devotion::getDevotionName, devotion.getDevotionName()));
        List<ExportVO> collect = list.stream().map(m -> {
            ExportVO exportVO = new ExportVO();
            BeanUtil.copyProperties(m, exportVO);
            return exportVO;
        }).collect(Collectors.toList());
        String[][] data = new String[collect.size() + 1][6];
        data[0] = new String[]{"投入品类型", "投入品名称", "规格","计量单位", "库存数量", "库存成本"};
        for (int i = 0; i < collect.size(); i++) {
            ExportVO exportVO = collect.get(i);
            data[i + 1] = new String[]{
                    exportVO.getBrandType(),
                    exportVO.getBrandName(),
                    exportVO.getSpecification(),
                    exportVO.getUnit(),
                    String.valueOf(exportVO.getNumber()),
                    String.valueOf(exportVO.getNumberCost())
            };
        }
        return ExcelExporter.export(data);

    }


}
